﻿using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain.RaiseManagement;
using FarmSmartSleep.Common.Domain.Sales_Purchase;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.InventoryCommands.InventoryStatistics
{
    /// <summary>
    /// 统计
    /// </summary>
    public class InventoryStatisticsHandler : IRequestHandler<InventoryStatisticsCommon, Result>
    {


        private readonly IBaseRepository<ProBegin> proBeginRep;               //后裔期初
        private readonly IBaseRepository<FemaleBegin> femaleBeginRep;         //种母期初
        private readonly IBaseRepository<MaleBegin> maleBeginRep;             //种公期初
        private readonly IBaseRepository<SalesRecord> salesRecordRep;         //销售表
        private readonly IBaseRepository<SheepRaiseDeath> sheepRaiseDeathRep; //死亡表
        private readonly IBaseRepository<SheepCulled> sheepCulledRep;         //淘汰表

        public InventoryStatisticsHandler(IBaseRepository<ProBegin> proBeginRep, IBaseRepository<FemaleBegin> femaleBeginRep, IBaseRepository<MaleBegin> maleBeginRep, IBaseRepository<SalesRecord> salesRecordRep, IBaseRepository<SheepRaiseDeath> sheepRaiseDeathRep, IBaseRepository<SheepCulled> sheepCulledRep)
        {
            this.proBeginRep = proBeginRep;
            this.femaleBeginRep = femaleBeginRep;
            this.maleBeginRep = maleBeginRep;
            this.salesRecordRep = salesRecordRep;
            this.sheepRaiseDeathRep = sheepRaiseDeathRep;
            this.sheepCulledRep = sheepCulledRep;
        }
        /// <summary>
        /// 栏位的统计
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(InventoryStatisticsCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CodeEnum.search };
            try
            {
                var proBegin = proBeginRep.GetAll().ToList();                   //后裔期初
                var femaleBegin = femaleBeginRep.GetAll().ToList();             //种母期初
                var maleBegin = maleBeginRep.GetAll().ToList();                 //种公期初
                var salesRecord = salesRecordRep.GetAll().ToList();             //销售表
                var sheepRaiseDeath = sheepRaiseDeathRep.GetAll().ToList();     //死亡表
                var sheepCulled = sheepCulledRep.GetAll().ToList();             //淘汰表

                var Beginfemale = femaleBegin.Count();
                var Beginmale = maleBegin.Count();
                var Beginpro = proBegin.Count();
                var BeginSum = Beginfemale + Beginmale + Beginpro;
                var salesRecordSum = salesRecord.Count();
                var sheepRaiseDeathSum = sheepRaiseDeath.Count();
                var sheepCulledSum = sheepCulled.Count();
                var SheepSum = salesRecordSum + sheepRaiseDeathSum + sheepCulledSum;
                var EndPeriod = BeginSum - SheepSum;
                result.Data = new
                {
                    Beginfemale,              //后裔期初                  
                    Beginmale,                //种母期初
                    Beginpro,                 //种公期初
                    BeginSum,                 
                    salesRecordSum,           
                    sheepRaiseDeathSum,       //销售表
                    sheepCulledSum,           //死亡表
                    SheepSum,                 //淘汰表
                    EndPeriod
                };
            }
            catch (Exception)
            {

                throw;
            }
            return result;
         



        }
    }
}
